package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;


    // http://localhost:8080/supplier/list

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping("list")
    public Map list(Integer page, Integer rows, String supplierName){
        Map<String, Object> map = new HashMap<>();
        PageInfo<Supplier> pageInfo = supplierService.list(page,rows,supplierName);
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }

    // http://localhost:8080/supplier/save?supplierId=1
    /**
     * 供应商添加或修改
     * @param supplier
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(Supplier supplier){
        supplierService.save(supplier);
        return new ServiceVO(100,"请求成功");
    }

    // http://localhost:8080/supplier/delete

    /**
     * 删除供应商（支持批量删除）
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(String[] ids){
        supplierService.delete(ids);
        return new ServiceVO(100,"删除成功");
    }

}
